package com.example.birdfriends.controller;

import ch.qos.logback.core.util.FileUtil;
import com.example.birdfriends.utils.result.Result;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private Result result;

    @PostMapping("/upload")
    public Map<String, Object> upload(@RequestParam("file") MultipartFile file) throws IOException {
        System.out.println("file name: "+file.getOriginalFilename());

        return result.resultMap(true, "上传视频", file);
    }

    @GetMapping("/download/{fileName}")
    public void download(@PathVariable("fileName") String fileName, HttpServletResponse response) throws IOException {
        String filePath = "D:/"+File.separator+fileName;

        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(new byte[1024]);
        outputStream.flush();
        outputStream.close();
    }
}
